const fs = require('fs');
const path = require('path');
const chokidar = require('chokidar');
const { PDFLoader } = require('@langchain/community/document_loaders/fs/pdf');
const { TextLoader } = require('langchain/document_loaders/fs/text');
const { DocxLoader } = require('@langchain/community/document_loaders/fs/docx');
const BaseRagService = require('./baseRag');
const VectorStoreService = require('../vectorStore');

class FileRagService extends BaseRagService {
  static RAW_DATA_DIR = path.join(process.cwd(), 'core', 'rag', 'data', 'files');
  static COLLECTION_NAME = 'fileRag';

  // 初始化数据
  static async initializeData() {
    try {
      // 确保目录存在
      if (!fs.existsSync(this.RAW_DATA_DIR)) {
        fs.mkdirSync(this.RAW_DATA_DIR, { recursive: true });
      }
      const ifExist = VectorStoreService.vectorStoreCollectionExists(this.COLLECTION_NAME)
      //判断向量存储的数据是否存在 不存在则处理文档生成
      if(!ifExist){
        // 处理现有文档
        await this.processDocuments();
      }else {
        // 如果已经有了则直接绑定已经有的
        await VectorStoreService.initCollection(this.COLLECTION_NAME);
        console.log('文件RAG集合数据已存在,将会使用原有的集合！');
      }
    } catch (error) {
      console.error('初始化数据失败:', error);
      throw error;
    }
  }

  // 处理文档列表
  static async processDocuments() {
    const files = fs.readdirSync(this.RAW_DATA_DIR);
    for (const file of files) {
      await this.processDocument(path.join(this.RAW_DATA_DIR, file));
    }
  }

  // 处理单个文档
  static async processDocument(filePath) {
    try {
      const ext = path.extname(filePath).toLowerCase();
      let loader;

      // 根据文件类型选择加载器
      switch (ext) {
        case '.pdf':
          loader = new PDFLoader(filePath);
          break;
        case '.txt':
          loader = new TextLoader(filePath);
          break;
        case '.docx':
          loader = new DocxLoader(filePath);
          break;
        case '.md':
        case '.markdown':
          loader = new TextLoader(filePath);
          break;
        default:
          console.log(`不支持的文件类型: ${ext}`);
          return;
      }

      // 加载文档
      const docs = await loader.load();
      
      // 添加到向量存储
      await this.processContent(docs, this.COLLECTION_NAME, {
        file_path: filePath,
        file_type: ext.slice(1)
      });
      
      console.log(`文档处理完成: ${filePath}`);
    } catch (error) {
      console.error(`处理文档失败 ${filePath}:`, error);
    }
  }
}

module.exports = FileRagService;